<template>
  <!-- 个人获奖及专利统计 -->
  <div class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['clr:award:applyAward']"
          >申请</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clr:batch:edit']"
        >修改</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['clr:batch:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table :data="blackList">
      <el-table-column
        label="学院"
        align="center"
        prop="collegeName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="clrName">
      </el-table-column>
      <el-table-column label="职称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.basic_title_type"
            :value="scope.row.title"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="国家级奖项名称"
        align="center"
        prop="natAwardName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="省级奖项名称"
        align="center"
        prop="provAwardName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="校级奖项名称"
        align="center"
        prop="univAwardName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="获奖年月"
        align="center"
        prop="awardDate"
        width="100"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="获奖类型"
        align="center"
        prop="awardType"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.clr_award_type"
            :value="scope.row.awardType"
          />
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="attachment">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleExport(scope.row)"
            v-if="scope.row.attachment"
            >下载附件</el-button
          >
          <el-button size="mini" type="text" v-if="!scope.row.attachment"
            >暂无附件</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" @click="handleEdit(scope.row)"   v-hasPermi="['clr:award:query']">修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['clr:award:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 申请 -->
    <el-dialog
      :title="titles"
      :visible.sync="open"
      width="60%"
      append-to-body
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="学院" prop="collegeName">
              <el-select
                v-model="form.collegeName"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="clrName">
              <el-input v-model="form.clrName" placeholder="请输入姓名" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职称" prop="title">
              <el-select
                  v-model="form.title"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.basic_title_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="国家级奖项名称" prop="natAwardName">
              <el-input
                v-model="form.natAwardName"
                placeholder="请输入国家级奖项名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省级奖项名称" prop="provAwardName">
              <el-input
                v-model="form.provAwardName"
                placeholder="请输入省级奖项名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校级奖项名称" prop="univAwardName">
              <el-input
                v-model="form.univAwardName"
                placeholder="请输入校级奖项名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="获奖年月" prop="awardDate">
              <el-date-picker
                v-model="form.awardDate"
                type="month"
                value-format="yyyy-MM"
                placeholder="请选择时间"
              >
              </el-date-picker>
              <!-- <yearTime @input="timeDate" :value="amcYear"></yearTime> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="获奖类型" prop="awardType">
              <el-select
                v-model="form.awardType"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.clr_award_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item> </el-col
          >。
          <el-col :span="12">
            <el-form-item label="附件" prop="attachment">
              <uploud ref="uploadList" @input="changeLoud"></uploud>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
      <script>
import { collegeSelect } from "@/api/information/studentInfor";
import {
  awardList,
  awardAdd,
  awardUpdata,
  getAward,
  delAward,
} from "@/api/clr/petentList";
import uploud from "./components/uploud.vue";
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
import Validators from "@/utils/validate.js";
import { downloadGet } from "@/utils/request";
export default {
  name: "patent",
  components: { searchForm, uploud },
  dicts: [
    "dep_grad_scope",
    "basic_title_type", // 职称类型
    "clr_award_type", // 获奖类型
  ],
  data() {
    return {
      formLabelWidth: "110px",
      formOptions: [
        {
          label: "学院",
          prop: "deptId",
          element: "el-select",
          options: [],
          placeholder: "请选择",
        },
        {
          label: "姓名",
          prop: "clrName",
          element: "el-input",
          placeholder: "请输入姓名",
        },
        {
          label: "职称",
          prop: "title",
          element: "el-select",
          options: [],
          placeholder: "请选择",
        },
        {
          label: "国家级奖项名称",
          prop: "natAwardName",
          element: "el-input",
          placeholder: "请输入国家级奖项名称",
        },
        {
          label: "省级奖项名称",
          prop: "provAwardName",
          element: "el-input",
          placeholder: "请输入省级奖项名称",
        },
        {
          label: "校级奖项名称",
          prop: "univAwardName",
          element: "el-input",
          placeholder: "请输入校级奖项名称",
        },
        {
          label: "获奖时间",
          prop: "applyTime",
          type: "monthrange",
          element: "el-date-picker",
          placeholder: "请选择时间",
        },
        // {
        //   label: "获奖时间",
        //   prop: "awardDate",
        //   type: "date",
        //   element: "el-date-picker",
        //   placeholder: "请选择时间"
        // },
        {
          label: "获奖类型",
          prop: "awardType",
          element: "el-select",
          options: [],
          placeholder: "请选择",
        },
      ],
      // 字典表格数据
      blackList: [],
      total: 0,
      form: {
        awardDate: "",
      },
      titles: "",
      // 显示搜索条件
      showSearch: true,
      open: false, // 弹窗开关
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      deptOptions: [], // 院系数据
      rules: {
        // collegeName: [
        //   { required: true, message: "请选择所属院系", trigger: "change" }
        // ],
        // clrName: [
        //   {
        //     required: true,
        //     text: "请输入姓名",
        //     validator: Validators.isString,
        //     trigger: "blur"
        //   }
        // ],
        // title: [
        //   { required: true, message: "请选择职称", trigger: "change" }
        // ],
        natAwardName: [
          {
            required: false,
            text: "请输入国家级奖项名称",

            trigger: "blur",
          },
        ],
        provAwardName: [
          {
            required: false,
            text: "请输入省级奖项名称",

            trigger: "blur",
          },
        ],
        univAwardName: [
          {
            required: false,
            text: "请输入校级奖项名称",

            trigger: "blur",
          },
        ],
        awardDate: [
          { required: true, message: "请选择获奖时间", trigger: "change" },
        ],
        awardType: [
          { required: true, message: "请选择获奖类型", trigger: "change" },
        ],
      },
      dateRange: [], // 时间
      fileList: [],
    };
  },
  created() {
    getDicts("basic_title_type").then((res) => {
      this.formOptions[2].options = res.data.map((item) => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"],
        };
      });
    });
    getDicts("clr_award_type").then((res) => {
      this.formOptions[7].options = res.data.map((item) => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"],
        };
      });
    });
  },
  computed: {
    userInfor() {
      return this.$store.state.user.userInfor;
    },
  },
  mounted() {
    this.getList();
    this.department();
  },
  methods: {
    // 获取搜索表单提交的数据
    onSearch(val) {
      if (val.applyTime) {
        this.dateRange = val.applyTime;
      }
      this.queryParams = Object.assign((this.queryParams, val));
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      data.params = {};
      this.dateRange = [];
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
      this.getList();
    },

    getList() {
      awardList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          console.log(response, "response");
          this.blackList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 上传
    changeLoud(data) {
      console.log(data, "数据");
      this.form.attachment = data;
    },
    handleEdit(row) {
      console.log(row);
      getAward(row.caaId).then((res) => {
        console.log(res, "详情");
        this.form = res.data;
        this.titles = "修改";
        this.open = true;
      });
    },
    handleDelete(row) {
      this.$modal
        .confirm(
          '是否确认删除"' + row.clrName + '"的个人获奖及专利统计申请的数据项？'
        )
        .then(function () {
          return delAward(row.caaId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.fileList = [];
        if (valid) {
          // this.form.clrExamineStaffList = this.clrExamineStaffList;
          if (this.form.caaId != null) {
            awardUpdata(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            awardAdd(this.form).then((response) => {
              this.$modal.msgSuccess("申请成功");
              this.open = false;
              this.getList();
            });
          }
          this.$refs.uploadList.uploadMethod();
        }
      });
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    // 获取部门信息
    async department() {
      await collegeSelect().then((res) => {
        this.deptOptions = res.data;
        this.formOptions[0].options = res.data.map((item) => {
          return {
            value: item["deptId"],
            label: item["deptName"],
          };
        });
      });
    },
    saveAs(obj, fileName) {
      let ele = document.createElement("a");
      ele.download = fileName || "下载"; //下载文件名
      ele.href = URL.createObjectURL(obj); // 绑定a标签
      ele.style.display = "none";
      document.body.appendChild(ele); // 兼容火狐浏览器
      ele.click();
      setTimeout(function () {
        // 延时释放
        URL.revokeObjectURL(obj); // 用 URL.revokeObjectURL() 来释放这个object URL
        document.body.removeChild(ele); // 兼容火狐浏览器
      }, 100);
    },
    // 新增
    handleAdd() {
      this.open = true;
      this.titles = "申请";
      this.form.clrName = this.userInfor.nickName;
      this.form.title = this.userInfor.teacher.title;
    },
    // 下载附件
    /** 导出按钮操作 */

    // 下载附件
    handleExport(row) {
      let link = document.createElement("a");
      //创建一个a标签
      link.style.display = "none";
      //将a标签隐藏
      link.href = process.env.VUE_APP_BASE_API + row.attachment;
      //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
      link.setAttribute("download", row.clrName + "个人获奖及专利");
      // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
      document.body.appendChild(link);
      //将上面创建的a标签加入到body的尾部
      link.click();
      // 执行a标签
    },
    // handleExport(row) {
    //     if (row.attachment) {  
    //       downloadGet(
    //         "/common/download",
    //         {
    //           params:{
    //             fileName: row.attachment
    //           }
    //         }, 
    //       );        } else {
    //       this.$modal.msgWarning("未找到上传结果");
    //     }
    //    }
    // handleExport(row) {
    //   console.log(row.attachment,'djkfjskj')
    //   let link = document.createElement("a");
    //   //创建一个a标签
    //   link.style.display = "none";
    //   //将a标签隐藏
    //   link.href = row.attachment;
    //   //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
    //   link.setAttribute("download", '下载上传结果');
    //   // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
    //   document.body.appendChild(link);
    //   //将上面创建的a标签加入到body的尾部
    //   link.click();
    //   //执行a标签

    // }
  },
};
</script>